package com.sunnyatwal.kettlebell;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunnyatwal.kettlebell.model.ExerciseUnit;
import com.sunnyatwal.kettlebell.model.SubExercise;

public class SubExerciseAdapter extends ArrayAdapter<ExerciseUnit> {

	private Context mContext; 
	private int mLayoutResourceId;    
	private List<ExerciseUnit> mData;
	
	
	static class SubExerciseHolder {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtTime;
        TextView txtWeight;
        TextView txtReps;
    }
	
	
	public SubExerciseAdapter(Context context, int layoutResourceId, List<ExerciseUnit> list) {
		
        super(context, layoutResourceId, list);
        this.mLayoutResourceId = layoutResourceId;
        this.mContext = context;
        this.mData = list;
    }
	
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		
		View row = convertView;
        SubExerciseHolder holder = null;
        
        if(row == null) {
        	
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            row = inflater.inflate(mLayoutResourceId, parent, false);
            
            holder = new SubExerciseHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtTime = (TextView)row.findViewById(R.id.txtTime);
            holder.txtWeight = (TextView)row.findViewById(R.id.txtWeight);
            holder.txtReps = (TextView)row.findViewById(R.id.txtReps);
            
            row.setTag(holder);
        }
        else {
            holder = (SubExerciseHolder)row.getTag();
        }
        
        ExerciseUnit exercise = mData.get(position);
        	                
        holder.txtTitle.setText(exercise.getName());
        holder.imgIcon.setImageResource(exercise.getIcon());
        
        holder.txtTime.setText(exercise.getTotalTimePretty(true));
        
        if(exercise instanceof SubExercise) {
        	SubExercise subExe = (SubExercise)exercise;
        	holder.txtWeight.setText(subExe.getWeightPretty(true));       
        	holder.txtReps.setText(subExe.getTotalRepsPretty(true));
        }
        
        return row;
    }
    
    
}

